import Vue from 'vue';
import routes from "router";
import Vonic from 'vonic';
import axios from 'axios';
import "config/rem.js";
import { DatetimePicker } from 'mint-ui';
// import Mint from 'mint-ui';
// Vue.use(Mint);
// import {DatePicker,TimePicker} from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css';

// Vue.use(DatePicker)
// Vue.use(TimePicker)
import 'mint-ui/lib/style.css'

// axios.defaults.baseURL = 'https://api.huoxingy.com/v1/admin/';

// 根据环境来选择api地址
let baseUrl = window.location.hostname;
console.log(baseUrl)
switch (baseUrl) {
    case "localhost":
        axios.defaults.baseURL = 'https://staging-api.huoxingy.com/v1/admin/';
        break;
    case "10.0.0.19":
        axios.defaults.baseURL = 'https://api.huoxingy.com/v1/admin/';
        break;
    case "staging-boss.huoxingy.com":
        axios.defaults.baseURL = 'https://staging-api.huoxingy.com/v1/admin/';
        break;
    case "prod-test-boss.huoxingy.com":
        axios.defaults.baseURL = 'https://prod-api.huoxingy.com/v1/admin/';
        break;
    default:
        axios.defaults.baseURL = 'https://prod-api.huoxingy.com/v1/admin/';
        break;
}
//



// axios.defaults.baseURL = 'http://192.168.0.106:9090/v1/admin/';
axios.defaults.headers.post['Content-Type'] = 'application/json';

/*
    设置请求拦截器
*/
axios.interceptors.request.use(function(config) {
    let token = localStorage.getItem('token');
    if (token) {
        config.headers.Authorization = token;
    }
    return config;
}, function(error) {
    return Promise.reject(error);
});

/*
    添加响应拦截器
*/
axios.interceptors.response.use(function(response) {
    return response;
}, function(error) {
    if (error.response) {
        let tipText;
        error.response.data.message?tipText=error.response.data.message:tipText=error.response.data;
        if ( error.response.status == 401 ) {
            window.location.href = window.location.href.split('#')[0]+'#/login';
            tipText = '帐号失效，请重新登录';
        } else if ( error.response.status == 403 ) {
            tipText = '权限不足';
        }else if ( error.response.status == 404 ) {
            tipText = '请尽快上传资料';
        }
        $toast.show(tipText, 2000).then(() => {});
    }
    return Promise.reject(error);
});
Vue.prototype.$uuid = function ( len, radix ) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [], i;
    radix = radix || chars.length;
    if (len) {
        // Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
    } else {
        // rfc4122, version 4 form
        var r;
 
        // rfc4122 requires these characters
        // uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
 
        // Fill in random data.  At i==19 set the high bits of clock sequence as
        // per rfc4122, sec. 4.1.5
        for ( i = 0; i < 32; i++ ) {
            if ( !uuid[i] ) {
                r = 0 | Math.random()*62;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
}
Vue.prototype.$http = axios;

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


// Services
// import $sidebar from './services/sidebar/index.js'
// Services
// Vue.prototype.$sidebar = window.$sidebar = $sidebar

// Components
import Datepicker from './components/datepicker'
// Components
Vue.component('datepickerC', Datepicker)


const is_ios = () => {
  return /iPad|iPhone|iPod/.test(navigator.userAgent)
}

Vue.filter('numberToMonthDate', function ( val ) {
    if ( !val ) {
        return '';
    }
    let date = new Date(val);
    let dateStr = ((date.getMonth()+1)<10?('0'+(date.getMonth()+1)):(date.getMonth()+1))+'.'+(date.getDate()<10?'0'+date.getDate():date.getDate())+'-'+ (date.getHours()<10?'0'+date.getHours():date.getHours()) + ':' + (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes());
    return dateStr;
})

Vue.filter('toFixedNum', function (value) {
    // if (!value) return 0;
    value = Number(value);
    return value.toFixed(2);
})

// 一些兼容性代码
/* for iOS 10, users can now pinch-to-zoom even when a website sets user-scalable=no in the viewport. */
document.documentElement.addEventListener('touchstart', (e) => {
  if (e.touches.length > 1) {
    e.preventDefault()
  }
}, false)

/* iOS Safari - Disable double click to zoom */
if (is_ios()) {
  let lastTouchEnd = 0;
  document.documentElement.addEventListener('touchend', (e) => {
    let now = (new Date()).getTime()
    if (now - lastTouchEnd < 300) {
      e.preventDefault()
    }
    lastTouchEnd = now
  }, false)
}
Vue.prototype.$canClickSelect = (sessionStorageName,timeStr,nowTime)=>{
    let sessionStorageData = sessionStorage.getItem(sessionStorageName);
    let defaultTimeRange = 30*1000;//默认30秒
    if(sessionStorageData){
        let localData = JSON.parse(sessionStorageData);
        let timeRange = nowTime - localData.localTime;
        console.log(localData.localTime)
        if(localData.timeStr==timeStr && timeRange < defaultTimeRange){
            return false;
        }
        let data = {
            sessionStorageName,
            timeStr,
            localTime:nowTime
        }
        sessionStorage.setItem(sessionStorageName,JSON.stringify(data));
        return true;
    }else{
        // 首次点击的时间
        let localTime = new Date().getTime();
        // 
        let data = {
            sessionStorageName,
            timeStr,
            localTime
        }
        sessionStorage.setItem(sessionStorageName,JSON.stringify(data));
        return true;
    }
};

Vue.component(DatetimePicker.name, DatetimePicker);

Vue.use(Vonic.app, {
  routes
})

